import os
import sys
import time
import argparse
import base64
import random
sys.path.append(os.getcwd())

import svgwrite
import json
from PIL import Image

from utils.font_size import *
from utils.image import *
from utils.platform import *
from utils.io import *
from components.text import *
from components.rect import *
from components.factory import *
from settings import *

assert is_windows() or is_darwin()

def gen_service(data):
    safe_area_size = 20 * SCALE
    icon_height = 100 * SCALE
    text_size = 40 * SCALE
    min_width = icon_height * 2 + safe_area_size * 2

    data_file_name = 'sa_pa_{}_{}'.format(time.strftime('%Y%m%d%H%M%S'), random.randint(0, 9999))
    svg_file_path = './output/{}.svg'.format(data_file_name)
    png_file_path = './output/{}.png'.format(data_file_name)

    icons = data.get('icons')
    icon_imgs = []
    for icon in icons:
        icon_file_path = './resources/{}.png'.format(icon)
        icon_img = Image.open(icon_file_path)
        icon_img = icon_img.resize((int(icon_height * (icon_img.width / icon_img.height)), int(icon_height)))
        icon_imgs.append(icon_img)

    item_type = data.get('name').get('type')
    item_info = data.get('name').get('info')

    factory = ComponentFactory(text_size, COLOR_WHITE)
    ele = factory.component(item_type, item_info)

    content_width = max(min_width, max(sum([img.width for img in icon_imgs]), ele.width() + 2 * safe_area_size + text_size) + 2 * safe_area_size)
    content_height = icon_height + ele.height() + 2 * safe_area_size

    dwg = svgwrite.Drawing(svg_file_path, profile='tiny', width=content_width, height=content_height)

    bg_rect = DoubleStrokeRectComponent(width=content_width, height=content_height, main_color=COLOR_GREEN, stroke_color=COLOR_WHITE, stroke_width=4 * SCALE, r=16 * SCALE)
    bg_rect.draw(dwg, 0, 0)
    dwg.add(dwg.rect(
        insert=(4 * SCALE, 4 * SCALE), 
        size=(content_width - 2 * 4 * SCALE, icon_height + 4 * SCALE), 
        rx=16 * SCALE - 4 * SCALE, 
        ry=16 * SCALE - 4 * SCALE, 
        fill=COLOR_WHITE
    ))
    dwg.add(dwg.rect(
        insert=(4 * SCALE, 4 * SCALE + icon_height + 4 * SCALE - (16 * SCALE - 4 * SCALE)), 
        size=(content_width - 2 * 4 * SCALE, 16 * SCALE - 4 * SCALE), 
        rx=0, 
        ry=0, 
        fill=COLOR_WHITE
    ))

    text_x = safe_area_size
    text_y = content_height - safe_area_size - ele.height()
    ele.draw(dwg, text_x, text_y)

    svg2png(dwg, content_width, content_height, svg_file_path, png_file_path)

    exit_img_path = './resources/exit.png'
    dst = Image.open(png_file_path)
    exit_img = Image.open(exit_img_path)
    exit_img = exit_img.resize((int(text_size), int(text_size)))
    exit_img_x = int(content_width - safe_area_size - text_size - (ele.height() - text_size) / 2)
    exit_img_y = int(content_height - safe_area_size - ele.height() + (ele.height() - text_size) / 2)
    dst.paste(exit_img, (exit_img_x, exit_img_y), exit_img)

    icon_y = int(2 * 4 * SCALE)
    icon_x = int((content_width - sum([img.width for img in icon_imgs])) / 2)
    for icon_img in icon_imgs:
        dst.paste(icon_img, (icon_x, icon_y), icon_img)
        icon_x += icon_img.width

    save_image(dst, png_file_path)
    with open(png_file_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
    return encoded_string, png_file_path


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--data', dest='Data', type=str, default='./example/sa_pa.json', help='Path of json data file.')
    args = parser.parse_args()
    data_file_path = args.Data
    with open(data_file_path, 'r', encoding='utf8') as file:
        data = json.load(file)
    gen_service(data)